# -*- coding:utf-8 -*-

import MySQLdb
import time

class MySQL:
    # 获取当前时间
     def getCurrentTime(self):
        return time.strftime('[%Y-%m-%d %H:%M:%S]',time.localtime(time.time()))

    # 数据库初始化
     def __init__(self):
         try:
             self.db = MySQLdb.connect(host="localhost",user="root",passwd="1994",db="aiwen",charset="utf8")
             self.cur = self.db.cursor()
         except MySQLdb.Error,e:
             print self.getCurrentTime(),"数据库链接失败，原因%d:%s" %(e.args[0],e.args[1])

    # 插入数据库
     def insertData(self,table,my_dict):
         try:
            self.db.set_character_set('utf-8')
            clos = ",".join(my_dict.values())
            values = '"," '.join(my_dict.values())
            sql = "INSERT INTO %s (%s) VALUES (%s)" % (table, clos, '"'+values+'"')
            try:
                result = self.cur.execute(sql)
                insert_id = self.db.insert_id()
                self.db.commit()
                # 判断是否执行成功
                if result:
                    return insert_id
                else:
                    return 0
            except MySQLdb.Error,e:
                # 发生错误时回滚
                self.db.rollback()
                if "key 'PRIMARY'" in e.args[1]:
                    print self.getCurrentTime(),"数据已经存在，未插入数据"
                else:
                    print self.getCurrentTime(),"插入数据失败，原因%d %s" %(e.args[0],e.args[1])
         except MySQLdb.Error,e:
             print self.getCurrentTime(),"数据库错误，原因%d %s" %(e.args[0],e.args[1])



